package com.yc.privilege.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yc.privilege.entity.RoleFunction;
import com.yc.privilege.mapper.RoleFunctionMapper;
import com.yc.privilege.service.RoleFunctionservice;
@Service("roleFunctionservice")
public class RoleFunctionserviceimpl implements RoleFunctionservice {
	
	@Autowired
	private RoleFunctionMapper roleFunctionMapper;
	
	@Override
	public RoleFunction findRoleFunctionById(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<RoleFunction> findRoleFunctionsByRoleId(int rid) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int deleteByRoleId(int rid) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean saveRoleFunction(RoleFunction roleFunction) {
		// TODO Auto-generated method stub
//		return roleFunctionMapper.saveRoleFunction(roleFunction);
		return true;
	}

	@Override
	public int saveRoleFunction(List<RoleFunction> role_Function_list) {
		return roleFunctionMapper.saveRoleFunction(role_Function_list);
	}
	/**
	 * 修改角色的权限功能，（想法是先删除原有的权限再进行添加）
	 * @param rid：角色id
	 * @param fids功能id字符串数组
	 * @return
	 */
	@Override
	public boolean updateRoleGrant(int rid, String fids) {
		int result = roleFunctionMapper.deleteByRoleId(rid);//删除原有的角色权限关系
		if(result>0){
			List<RoleFunction> list=new ArrayList<RoleFunction>();
			RoleFunction roleFunction=null;
			String[] strs = fids.split(",");
			int j=strs.length;
			for(int i=0;i<j;i++){
				roleFunction=new RoleFunction(rid,Integer.parseInt( strs[i]));
				list.add(roleFunction);
			}
			  int result3 = saveRoleFunction(list);//添加更新的角色权限关系
				if(j==result3){
					return true;
				}
		}
		return false;
	}
	
	

}
